package cn.edu.cdut.service;

import cn.edu.cdut.dao.StudentInfoDao;
import cn.edu.cdut.dao.WishInfoDao;
import cn.edu.cdut.dto.MyRes;
import cn.edu.cdut.dto.Wish;
import cn.edu.cdut.model.StudentInfo;
import cn.edu.cdut.model.WishInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * Author  kobe
 * Created on 2017-05-27-22:50
 */
@Service
public class WishService {
        @Autowired
        private WishInfoDao wishInfoDao;

        @Autowired
        private StudentInfoDao studentInfoDao;

        //保存志愿
        @Transactional
        public MyRes saveWish(Wish wish) {

                WishInfo wishInfoOld = wishInfoDao.findByStuId(wish.getStuId());

                if (wishInfoOld == null) {
                        StudentInfo studentInfo = studentInfoDao.findByStuId(wish.getStuId());
                        WishInfo wishInfo = new WishInfo();
                        wishInfo.setGrade(studentInfo.getGrade());
                        wishInfo.setStuId(studentInfo.getStuId());
                        wishInfo.setStuName(studentInfo.getStuName());
                        wishInfo.setSex(studentInfo.getSex());
                        wishInfo.setScore(studentInfo.getScore());
                        wishInfo.setStuCollege(studentInfo.getStuCollege());
                        wishInfo.setStuSubject(studentInfo.getStuSubject());
                        wishInfo.setWish1(wish.getWish1());
                        wishInfo.setWish2(wish.getWish2());
                        wishInfo.setWish3(wish.getWish3());
                        wishInfo.setWish4(wish.getWish4());
                        wishInfo.setWish5(wish.getWish5());
                        wishInfoDao.save(wishInfo);
                        return new MyRes(200, "提交成功！", null);
                } else {
                        wishInfoOld.setWish1(wish.getWish1());
                        wishInfoOld.setWish2(wish.getWish2());
                        wishInfoOld.setWish3(wish.getWish3());
                        wishInfoOld.setWish4(wish.getWish4());
                        wishInfoOld.setWish5(wish.getWish5());
                        wishInfoDao.save(wishInfoOld);
                        return new MyRes(200, "提交成功！", null);
                }


        }
}
